const fs = require('fs');
const path = require('path');

// 读取当前目录
const dirs = fs.readdirSync('.');
let subDirs = [];
let subItems = [];
dirs.forEach(dir => {
  const stats = fs.statSync(dir);
  if (stats.isDirectory() && dir != '.git') {
    subDirs.push(dir);
    const subDir = `./${dir}`;
    const content_dir = fs.readdirSync(subDir);
    const list = [];
    content_dir.forEach(file => {
      if (file.endsWith('.md')) {
        list.push(file);
      }
    });
    let index_content = `## ${dir}\n`;
    let temp = [];
    list.forEach(item => {
      const subName = path.basename(item, '.md');
      const subItem = `- [${subName}](/${dir}/${item})\n`;
      temp.push(subItem);
      index_content += subItem;
    });
    subItems.push(temp);
    fs.writeFileSync(`${subDir}/index.md`, index_content);
  }
});
// 写入 summary.md
let data_summary = '\n';
subDirs.forEach((item, index) => {
  data_summary += `- [${item}](${item}/index.md)\n`;
  const subItem = subItems[index];
  subItem.forEach(sub => {
    data_summary += `\t ${sub}`;
  });
});
fs.appendFileSync('./SUMMARY.md', data_summary);
